package avicit.bdp.dcs.rule.rest;

import avicit.bdp.dcs.Constant;
import avicit.bdp.dcs.rule.dto.ColumnRuleDTO;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.alibaba.fastjson2.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import static org.junit.Assert.assertEquals;

public class ColumnRuleRestTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void saveColumnRule() {
        ColumnRuleDTO columnRuleDTO = new ColumnRuleDTO();
        columnRuleDTO.setTaskId(Constant.TASKID);
        columnRuleDTO.setRuleType(1);
        columnRuleDTO.setPrefix("test_");
        String param = JSONObject.toJSONString(columnRuleDTO);
        HttpHeaders headers = new HttpHeaders(); // 创建一个头部对象
        // 设置contentType 防止中文乱码
        headers.setContentType(MediaType.valueOf("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
        // 设置我们的请求信息，第一个参数为请求Body,第二个参数为请求头信息
        HttpEntity<String> httpEntity = new HttpEntity<String>(param, headers);
        RestTemplate restTemplate  = new RestTemplate();
        // 使用post方法提交请求，第一参数为url,第二个参数为我们的请求信息,第三个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(Constant.HOSTPORT + "/api/bdp/dcs/rule/ColumnRuleRest/saveColumnRule", httpEntity, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

    @Test
    public void deleteColumnRuleById() {
        RestTemplate restTemplate  = new RestTemplate();
        restTemplate.delete(Constant.HOSTPORT + "/api/bdp/dcs/rule/ColumnRuleRest/deleteColumnRuleById?columnRuleId=1", "");
    }

    @Test
    public void getColumnRuleListByTaskId() {
        RestTemplate restTemplate  = new RestTemplate();
        // 使用get方法提交请求，第一参数为url,第二个参数为相应返回数据类型
        ResponseEntity<ResponseMsg> result = restTemplate.getForEntity(Constant.HOSTPORT + "/api/bdp/dcs/rule/ColumnRuleRest/getColumnRuleListByTaskId?taskId="+Constant.TASKID, ResponseMsg.class);
        assertEquals("200", result.getBody().getRetCode());
    }

}
